﻿CREATE PROCEDURE [dbo].[ContragentsInsert]
(
	@Name varchar(100),
	@ShortName varchar(20),
	@IsSeller bit,
	@IsCustomer bit,
	@IsSertification bit,
	@EMail varchar(50),
	@Phone varchar(50),
	@DirectorFIO varchar(100),
	@DirectorPosition varchar(50),
	@Note varchar(250),
	@FirmType varchar(10),
	@JuridicalAddress varchar(250),
	@INN varchar(12),
	@KPP varchar(20),
	@OKPO varchar(20),
	@OKONX varchar(20),
	@SettlementAccount varchar(20),
	@Bank varchar(100),
	@BIK varchar(10),
	@CorrespondentAccount varchar(20),
	@ActualAddress varchar(250),
	@Fax varchar(50),
	@WebPage varchar(50),
	@ICQ varchar(50),
	@MD datetime,
	@MW varchar(50)
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @intNextId int
SET NOCOUNT ON

IF (@Name Is Null) OR (@ShortName Is Null) OR (@IsSeller Is Null) OR (@IsCustomer Is Null) OR (@IsSertification Is Null) 
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF (ltrim(@Name) = '') OR (ltrim(@ShortName) = '')
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Незаполнено наименование или короткое наименование!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF EXISTS (SELECT TOP 1 ID FROM [CA].[Contragents] WHERE Name = @Name OR ShortName = @ShortName AND ToDelete = 0) 
BEGIN
	SELECT @intErrCode = 3, @strMsg = 'Дублируется наименование или короткое наименование!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF NOT EXISTS (SELECT DepartmentId FROM PR.DepartmentOptions WHERE DepartmentId = 0 ) 
BEGIN
	SELECT @intErrCode = 4, @strMsg = 'Изменять данные спрвочников можно только в офисе!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
--получение значения идентификатора
EXEC @intRet = dbo.GetNextCounterFor 'Common', @intNextId OUTPUT
IF @intRet <> 0 RETURN @intRet

	SET NOCOUNT OFF;
INSERT INTO [CA].[Contragents] (Id, [Name], [ShortName], [IsSeller], [IsCustomer], [IsSertification], [EMail], [Phone], 
DirectorFIO, DirectorPosition, [Note], [FirmType], [JuridicalAddress], [INN], [KPP], [OKPO], [OKONX], [SettlementAccount], [Bank], [BIK], [CorrespondentAccount], [ActualAddress], [Fax], [WebPage], [ICQ],
[MD], [MW], ToDelete)
VALUES (@intNextId, @Name, @ShortName, @IsSeller, @IsCustomer, @IsSertification, @EMail, @Phone, 
@DirectorFIO, @DirectorPosition, @Note, @FirmType, @JuridicalAddress, @INN, @KPP, @OKPO, @OKONX, @SettlementAccount, @Bank, @BIK, @CorrespondentAccount, @ActualAddress, @Fax, @WebPage, @ICQ, 
@MD, @MW, 0);
	
SELECT Id, GId, Name, ShortName, IsSeller, IsCustomer, IsSertification, EMail, Phone, 
DirectorFIO, DirectorPosition, Note, FirmType, JuridicalAddress, INN, KPP, OKPO, OKONX, SettlementAccount, Bank, BIK, CorrespondentAccount, ActualAddress, Fax, WebPage, ICQ, 
MD, MW, ToDelete
FROM CA.Contragents WHERE (Id = @intNextId)

RETURN 0
